﻿(function () {
    "use strict";

    function init(key, title, subtitle, description, apiKey, photosetId) {
        this.key = key;
        this.title = title;
        this.subtitle = subtitle;
        this.description = description;
        this.apiKey = apiKey;
        this.photosetId = photosetId;
    }

    var instanceMembers = {
        getFeed: function () {
            var today = new Date();

            var feed = {
                backgroundImage: "http://l.yimg.com/g/images/es-us/flickr-yahoo-logo.png.v4",
                description: this.description,
                key: this.key,
                logoUrl: "http://l.yimg.com/g/images/es-us/flickr-yahoo-logo.png.v4",
                pubDate: today.getDay + "-" + today.getMonth + "-" + today.getFullYear(),
                subtitle: this.subtitle,
                title: this.title,
                url: "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=" + this.apiKey + "&photoset_id=" + this.photosetId + "&format=json&nojsoncallback=1",
            };

            return WinJS.xhr({ url: feed.url }).then(function (response) {
                var result = JSON.parse(response.responseText);
                var feedItems = [];
                var postIndex = 0;

                result.photoset.photo.forEach(function (photo) {
                    var imgUrl = "http://farm" + photo.farm + ".staticflickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + "_z.jpg";
                    var feedItem = {
                        author: result.photoset.ownername,
                        backgroundImage: imgUrl,
                        content: "",
                        group: feed,
                        key: photo.title,
                        link: imgUrl,
                        postIndex: postIndex,
                        pubDate: "",
                        title: photo.title
                    };
                    feedItems.push(feedItem);
                    postIndex++;
                })

                return WinJS.Promise.wrap(feedItems);
            });
        },
    };

    var staticMembers = {};

    var FlickrPhotosetsGetPhotosQuery = WinJS.Class.define(init, instanceMembers, staticMembers);

    // add to namespace
    WinJS.Namespace.define("Queries",
        {
            FlickrPhotosetsGetPhotosQuery: FlickrPhotosetsGetPhotosQuery
        }
    );
})();